package com.krui.mp3player;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.krui.data.Mp3Info;
import com.krui.utils.HttpDownloader;

public class Mp3PlayerActivity extends ListActivity {
	/** Called when the activity is first created. */

	private static final int MENU_UPDATE = Menu.FIRST;
	private static final int MENU_SET = Menu.FIRST + 1;
	private static final int MENU_ABOUT = Menu.FIRST + 2;
	private static final int MENU_EXIT = Menu.FIRST + 3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, MENU_UPDATE, 1, R.string.mp3list_update).setIcon(
				R.drawable.menu_refresh);
		menu.add(0, MENU_SET, 2, R.string.mp3list_set).setIcon(
				R.drawable.menu_switchuser_setting);
		menu.add(0, MENU_ABOUT, 3, R.string.mp3list_about).setIcon(
				R.drawable.menu_switchuser_about);
		menu.add(0, MENU_EXIT, 4, R.string.mp3list_exit).setIcon(
				R.drawable.menu_exit);
		return super.onCreateOptionsMenu(menu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case MENU_UPDATE:
//			String xml = downloadXML("http://10.0.2.2:8080/mp3/resources.xml");
//			System.out.println("xml -----> " + xml);
			updateView();
			break;
		case MENU_SET:
			break;
		case MENU_ABOUT:
			break;
		case MENU_EXIT:
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private SimpleAdapter buildSimpleAdapter(List<Mp3Info> mp3Infos) {
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info mp3Info = (Mp3Info) iterator.next();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("mp3_name", mp3Info.getMp3Name());
			map.put("mp3_size", mp3Info.getLrcName());
			list.add(map);
		}
		// 创建一个simpleAdapter对象
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
				R.layout.mp3info_item, new String[] { "mp3_name", "mp3_size" },
				new int[] { R.id.mp3_name, R.id.mp3_size });
		return simpleAdapter;
	}

	public void updateView() {
		String xml = downloadXML("http://10.0.2.2:8080/mp3/resources.xml");
		System.out.println(xml);
		List<Mp3Info> mp3Infos = pase(xml);
		SimpleAdapter simpleAdapter = buildSimpleAdapter(mp3Infos);
		//ListView myListView = (ListView)findViewById(R.id.mylist);
		setListAdapter(simpleAdapter);
	}

	private List<Mp3Info> pase(String xmlStr) {
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		List<Mp3Info> infos = new ArrayList<Mp3Info>();
		try {
			XMLReader xmlReader = saxParserFactory.newSAXParser()
					.getXMLReader();
			Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(
					infos);
			xmlReader.setContentHandler(mp3ListContentHandler);
			xmlReader.parse(new InputSource(new StringReader(xmlStr)));
			// 迭代器的使用
			for (Iterator<Mp3Info> iterator = infos.iterator(); iterator
					.hasNext();) {
				Mp3Info mp3Info = (Mp3Info) iterator.next();
				System.out.println(mp3Info);
			}

		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return infos;
	}
	private String downloadXML(String urlStr) {
		HttpDownloader httpDownloader = new HttpDownloader();
		String result = httpDownloader.download(urlStr);
		return result;

	}

}